#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QThread>
#include <QMessageBox>
#include "worker.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,worker(new Worker(0))
{
    ui->setupUi(this);
    connect(&thread,&QThread::started,[=](){
        qDebug()<< QStringLiteral("线程启动了，并且启动了事件循环");
    });
    connect(&thread,&QThread::finished,[=](){
        qDebug()<< QStringLiteral("线程结束了");
    });
    worker->moveToThread(&thread);
    connect(this,&Widget::doTask,worker,&Worker::dowork);
    thread.start();
}

void Widget::on_pushButton_clicked()
{
    qDebug()<< QStringLiteral("主线程ID:")
            << QThread::currentThreadId();
    worker->setIndex(++index);

    emit this->doTask();

}


void Widget::on_pushButton_2_clicked()
{
    qDebug()<< QStringLiteral("调quit:");
    thread.quit();
}



Widget::~Widget()
{
    thread.quit();
    delete ui;
}
